﻿@model HomeControllerModel.List
<div>
    <ul id="auctionList">
    </ul>
</div>
<div id="message">
</div>

@section header{
}
@section footer{
}
@section js{
    @Url.Sk().StaticJs("jquery.tmpl.min")
    @Url.Sk().StaticJs("zmqsocket")
    <script id="auctiontmpl" type="text/x-jquery-tmpl">     
     <li>
         <div class="">
             <span id="counter-${Id}">${Remain}</span>
             <span id="currentprice-${Id}">${CurrentPrice}</span>
            <span id="currentuser-${Id}">${NickName}</span>
            <a  href="javascript:void(0);" onclick="bid('${Id}');">出价</a>   
            <div id="message-${Id}"></div>
           <a  href="javascript:void(0);" onclick="Sk.Passport.Fun.login();">登录</a>   
         </div>
     </li>
    </script>
    <script type="text/javascript">
        var uid = @(User.Identity.SkIdentity().Id) ;
        var ready = false;
        var socket = null;
        var auctions = {};
        $(function () {
            $.post('/home/auctions?r=' + Math.random(), function(result) {
                var json = eval('(' + result + ')');               
                $.each(json, function(i, n) {
                    var key = n.Id.toString();
                    auctions[key] = n;
                    auctions[key].Remain = n.Remain;
                });
                //console.log(json);
                //console.log(auctions);
                $("#auctiontmpl").tmpl(json).appendTo("#auctionList");
                setInterval(updateAuction, 1000);
              }); 
            
            var socket = new ZmqSocket("auction");
            socket.onopen = function () {
                ready = true;
            };
            socket.onmessage = function () {
            //alert(socket.available());
                if (socket.available()) {
                    var data = socket.recv();
                    //alert(data);
                    var json = eval('(' + data + ')');
                    console.log(json);
                if (json.Status == true) {
                    var bid = json.Data;
                    var key = json.Data.AuctionId.toString();
                    auctions[key].CurrentPrice = bid.CurrentPrice;
                    auctions[key].Remain = auctions[key].Interval;
                    auctions[key].UserId = bid.UserId;
                    auctions[key].CurrentUser = bid.NickName;
                }
                else {
                    notify(json.Message,json.Extra,true);
                }
                }
                else {
                    alert('Assertion failure!');
                }
            };
            socket.onclose = function () { alert('close'); };
            socket.onerror = function (msg) { alert(msg); };
            socket.connect("127.0.0.1", 5555);
        });

        function bid(id) {
            var auction = auctions[id];
            //alert(uid);
            //alert(auction.UserId);
            if(uid==auction.UserId) {
                notify("不能连接出价",id,true);
            }
            else {
                var url = '/bid/index/'+id+"?r="+Math.random();
                //alert(url);
                $.postJSON(url, function(result) {
                    var data = result;
                      if (data.Status == false)
                      {
                          alert(data.Message);
                      }
                    //var json = eval('(' + result + ')');   
                });
            }
        }
        
        function notify(msg,id,autoHide) {
            var elementId = "message";
            if(id>0) {
                elementId += "-" + id;
            }
            var msgElement = document.getElementById(elementId);
            $(msgElement).html(msg);
            if(autoHide) {
                setTimeout(function() { $(msgElement).empty(); }, 5000);
            }
        }
        
        function updateAuction() {
            $.each(auctions, function (i, n) {
                var now = new Date();
                var counterId = "counter-" + i;
                var currentPriceId = "currentprice-" + i;
                var currentUserId = "currentuser-" + i;
                var currentPriceElement = document.getElementById(currentPriceId);
                var currentUserElement = document.getElementById(currentUserId);
                var remainElement = document.getElementById(counterId);
                //alert(n.BeginTime);
                if (now < n.BeginTime) {
                    $(remainElement).text('还没开始');
                }
                else if (n.CurrentPrice == 0) {
                    $(remainElement).text('等待出价');
                }
                else {
                    var remain = n.Remain - 1;
                    auctions[i.toString()].Remain = remain;
                    if (remain > 0) {
                        $(remainElement).text(remain);
                    }
                    else {
                        $(remainElement).text('xxx');
                    }
                }
                $(currentPriceElement).text(n.CurrentPrice);
                $(currentUserElement).text(n.CurrentUser);

            });
        }
    </script>    
}
